package com.fonddream.CursorLoaderExample;

import android.app.Activity;
import android.app.LoaderManager;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import com.fonddram.loader.ContactCursorLoader;

public class MainActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor> {
    private static final int CONTACT_CURSOR_LOADER = 0;
    private SimpleCursorAdapter mSimpleCursorAdapter;
    private ListView mListView;
    private TextView title;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mListView = (ListView) findViewById(R.id.listView);
        title = (TextView) findViewById(R.id.title);
        getLoaderManager().initLoader(CONTACT_CURSOR_LOADER, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        return new ContactCursorLoader(this);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        String[] from = {
                ContactsContract.Contacts.DISPLAY_NAME
        };
        int[] to = {
                R.id.name
        };

        mSimpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.item_contact_info, cursor, from, to);
        mListView.setAdapter(mSimpleCursorAdapter);

        title.setText(getResources().getString(R.string.count, mSimpleCursorAdapter.getCount()));
    }

    @Override
    public void onLoaderReset(Loader<Cursor> cursorLoader) {


    }

    public static void test() {
    }
}
